/**
 * @desc 版本管理
 * @author yd_zhaorongjun
 */
<template>
  <div class="gl_contentBox gl_heightFull">
    <div class="demo-input-suffix gl_searchBox">
      <el-row :gutter="20">
        <!-- 版本名称： -->
        <el-col :md="6" :lg="4">
          <p>{{ $t('tenant.versionName') }}</p>
          <el-input v-model="Api_GET_EditionsQO.Filter" :placeholder="$t('tenant.placeholder_versionName')" size="mini" clearable @change="refreshList()" />
        </el-col>
        <!-- 查询按钮 -->
        <el-col :md="6" :lg="4">
          <el-button style="margin-top:27px" type="primary" size="mini" @click="refreshList()">{{ $t('button.query') }}</el-button>
        </el-col>
      </el-row>
    </div>
    <template>
      <!-- 添加版本按钮 -->
      <el-row :gutter="20" class="gl_my_15">
        <el-col :md="6" :lg="4">
          <el-button type="success" size="mini" @click="editOfAdd()">{{ $t('button.addVersion') }}</el-button>
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        size="mini"
        style="width: 100%;"
      >
        <!-- 版本名称 -->
        <el-table-column
          prop="displayName"
          :label="$t('table.versionName')"
          :tooltip="true"
          width="200"
        />
        <!-- 描述 -->
        <el-table-column
          :label="$t('table.description')"
          min-width="240"
        >
          <template slot-scope="{row}">
            <el-popover
              placement="top-start"
              width="500"
              trigger="hover"
              :content="row.description"
            >
              <span slot="reference" class="gl_ellipsis">{{ row.description }}</span>
            </el-popover>
          </template>
        </el-table-column>
        <!-- 创建时间 -->
        <el-table-column
          :label="$t('table.creationTime')"
          align="center"
          width="220"
        >
          <template slot-scope="{row}">
            <span v-if="row.creationTime">
              {{ row.creationTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}
            </span>
          </template>
        </el-table-column>
        <!-- 创建人 -->
        <el-table-column
          prop="creatorName"
          :label="$t('table.creator')"
          width="180"
        />
        <el-table-column fixed="right" :label="$t('table.actions')" width="180">
          <template slot-scope="{row,$index}">
            <!-- 编辑按钮 -->
            <el-button size="mini" type="text" @click="editOfAdd(row,$index)">
              {{ $t('table.edit') }}
            </el-button>
            <!-- 删除按钮 -->
            <el-button v-if="!row.hasUsed" size="mini" type="text" @click="clickDelete(row)">
              <span class="gl_color_error">{{ $t('table.delete') }}</span>
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-row class="gl_pt_20" style="text-align: center;">
        <el-pagination
          :current-page="Api_GET_EditionsQO.Page"
          :page-size="Api_GET_EditionsQO.MaxResultCount"
          :total="totalPage"
          layout="total,sizes,prev,pager,next,jumper"
          :page-sizes="[10, 20, 30, 40]"
          @size-change="sizeChange"
          @current-change="pageIndexChange"
        />
      </el-row>
    </template>

    <edit-Version ref="editVersion" @insert="versionInsert" @update="versionUpdate" />
  </div>
</template>

<script>

import { Api_GET_EditionsQO } from '@/lib/form/swaggerForm';
import { FeaturesQO } from '@/lib/form/tenantManagement.js';
import editVersion from './editVersion';
import { translateByCode } from '@/lib/getTransData';
import { api_GET_Editions, api_DELETE_EditionsId, api_POST_Editions, api_PUT_EditionsId } from '@/extend/api/swaggerApi/saas';
import { api_PUT_Features } from '@/extend/api/swaggerApi/feature-management';
export default {
  name: 'Version',
  components: {
    editVersion
  },
  data() {
    return {
      FeaturesQO: new FeaturesQO(),
      Api_GET_EditionsQO: new Api_GET_EditionsQO(),
      loading: false,
      totalPage: 0,
      tableData: []
    };
  },
  computed: {
  },
  created() {
    this.refreshList();
  },
  methods: {
    // 刷新列表
    refreshList(page) {
      this.loading = true;
      this.Api_GET_EditionsQO.Page = page || 1;
      this.Api_GET_EditionsQO.SkipCount = (this.Api_GET_EditionsQO.Page - 1) * this.Api_GET_EditionsQO.MaxResultCount;
      api_GET_Editions(this.Api_GET_EditionsQO).then(res => {
        console.log(res);
        this.tableData = res.items;
        this.totalPage = res.totalCount;
        this.loading = false;
      }, (error) => {
        this.loading = false;
        this.$message({ type: 'error', message: error.error.message });
      });
    },
    // 添加或修改版本
    editOfAdd(row) {
      this.editId = row ? row.id : '';
      this.$refs.editVersion.open(row);
    },
    // 删除按钮
    clickDelete(row) {
      // 您确定要删除 xxx 吗？ zh
      const _confirmStr = ` ${translateByCode('alert.wantDelete')} [ ${row.displayName} ] ${translateByCode('alert.ma')}`;
      this.$confirm(_confirmStr, translateByCode('alert.warning'), {
        confirmButtonText: translateByCode('alert.confirm'),
        cancelButtonText: translateByCode('alert.cancel'),
        type: 'warning'
      }).then(() => {
        this.loading = true;
        api_DELETE_EditionsId(row.id).then(res => {
          this.$message({
            type: 'success',
            // 删除成功！
            message: translateByCode('alert.deleteSuccessfully')
          });
          this.refreshList();
        }, (error) => {
          this.loading = false;
          _this.$message({ type: 'error', message: error.error.message });
        });
      });
    },
    // 添加版本
    versionInsert(data, treeArray) {
      const _this = this;
      api_POST_Editions(data).then(res => {
        console.log(res);
        _this.putFeatures(res.id, treeArray);
      }, (error) => {
        _this.$refs.editVersion.submitLoading = false;
        _this.$message({ type: 'error', message: error.error.message });
      });
    },
    // 修改版本
    versionUpdate(data, treeArray) {
      const _this = this;
      api_PUT_EditionsId(this.editId, data).then(res => {
        console.log(res);
        _this.putFeatures(res.id, treeArray);
      }, (error) => {
        _this.$refs.editVersion.submitLoading = false;
        _this.$message({ type: 'error', message: error.error.message });
      });
    },
    // 保存树结构数据
    putFeatures(id, treeArray) {
      const _this = this;
      this.FeaturesQO.providerKey = id;
      console.log(treeArray);
      api_PUT_Features(this.FeaturesQO, { features: treeArray }).then(res => {
        this.$message({
          // 添加成功！
          message: translateByCode('alert.addSuccessfully'),
          type: 'success'
        });
        _this.$refs.editVersion.dialogVisible = false;
        _this.$refs.editVersion.submitLoading = false;
        _this.refreshList();
      }, (error) => {
        _this.$refs.editVersion.submitLoading = false;
        _this.$message({ type: 'error', message: error.error.message });
      });
    },
    sizeChange(MaxResultCount) {
      this.Api_GET_EditionsQO.MaxResultCount = MaxResultCount;
      this.refreshList();
    },
    pageIndexChange(currentPage) {
      this.refreshList(currentPage);
    }
  }
};
</script>
